package com.itany.netclass.service.impl;

import com.itany.netclass.constant.CommentConstant;
import com.itany.netclass.dao.CommentMapper;
import com.itany.netclass.dao.ResourceMapper;
import com.itany.netclass.entity.Comment;
import com.itany.netclass.entity.Resource;
import com.itany.netclass.exception.NoQuanXianException;
import com.itany.netclass.exception.NullSringException;
import com.itany.netclass.factory.ObjectFactory;
import com.itany.netclass.service.CommentService;

import java.util.List;

/**
 * Author:sjy@itany.com
 * Date:2021/10/18 21:52
 * Version:1.0-SNAPSHOT
 */
public class CommentServiceImpl implements CommentService {
    @Override
    public List<Comment> findCommentByChapterId(String chapterId) throws NullSringException {
        CommentMapper commentMapper = (CommentMapper) ObjectFactory.getObject("CommentMapper");
        List<Comment> list=commentMapper.selectCommentByChapterId(chapterId);
        if(list.isEmpty()){
            throw new NullSringException("该章节下无任何评论或评论未审核通过");
        }
        return list;
    }

    @Override
    public List<Comment> searchCommentByForm(String chapterId,String context, String nickname, String kaishitime, String jieshutime, String status) throws NullSringException {
        CommentMapper commentMapper = (CommentMapper) ObjectFactory.getObject("CommentMapper");
        List<Comment> list=commentMapper.selectCommentBy(chapterId,context,nickname,kaishitime,jieshutime,status);
        if(!list.isEmpty()){
            return list;
        }else {
            throw new NullSringException("为查询到符合条件的评论");
        }
    }

    @Override
    public void yongCommentByComment(String commentId, String status) throws NoQuanXianException {
        ResourceMapper resourceMapper = (ResourceMapper) ObjectFactory.getObject("ResourceMapper");
        CommentMapper commentMapper = (CommentMapper) ObjectFactory.getObject("CommentMapper");
        Comment comment = commentMapper.selectByPrimaryKey(Integer.parseInt(commentId));
        Resource resource = resourceMapper.selectByPrimaryKey(comment.getResourceId());
        if(resource.getStatus()==-1
           &&
        status.equals(CommentConstant.COMMENT_STATUS_QIYONG)){
            throw new NoQuanXianException(resource.getTitle()+"已被禁用，其下的评论无法启用");
        }

        if(status.equals(CommentConstant.COMMENT_STATUS_QIYONG)){
            comment.setStatus(Integer.parseInt(CommentConstant.COMMENT_STATUS_QIYONG));
        }
        if(status.equals(CommentConstant.COMMENT_STATUS_JINYONG)){
            comment.setStatus(Integer.parseInt(CommentConstant.COMMENT_STATUS_JINYONG));
        }
        commentMapper.updateByPrimaryKeySelective(comment);

    }

    @Override
    public List<Comment> findManageComment() throws NullSringException {
        CommentMapper commentMapper = (CommentMapper) ObjectFactory.getObject("CommentMapper");
        List<Comment> list = commentMapper.selectManageComment();
        if (list.isEmpty()){
            throw new NullSringException("暂无未审核的评论");
        }
        return list;
    }

    @Override
    public List<Comment> searchManageByForm(String commentUserName, String context, String kaishitime, String jieshutime) throws NullSringException {
        CommentMapper commentMapper = (CommentMapper) ObjectFactory.getObject("CommentMapper");
        List<Comment> list = commentMapper.selectManageCommentByForm(commentUserName,context,kaishitime,jieshutime);
        if (list.isEmpty()){
            throw new NullSringException("暂无符合条件的评论");
        }
        return list;
    }

    @Override
    public void yongManageByCommentId(String commentId, String status) {
        Comment comment =new Comment();
        if(status.equals(CommentConstant.COMMENT_STATUS_QIYONG)){
            comment.setStatus(Integer.parseInt(CommentConstant.COMMENT_STATUS_QIYONG));
        }
        if(status.equals(CommentConstant.COMMENT_STATUS_JINYONG)){
            comment.setStatus(Integer.parseInt(CommentConstant.COMMENT_STATUS_JINYONG));
        }
        CommentMapper commentMapper = (CommentMapper) ObjectFactory.getObject("CommentMapper");
        comment.setId(Integer.parseInt(commentId));
        commentMapper.updateByPrimaryKeySelective(comment);

    }
}
